from imagehandling import ImageHandling
import Image
from resize_dialog import ResizeDialog

class TransformMenu:
    def __init__(self, editor, widgets):
        self.editor = editor
        
        # Fetch button widgets.
        rotate_left_button = widgets.get_widget('Editor:RotateLeftButton')
        rotate_right_button = widgets.get_widget('Editor:RotateRightButton')
        flip_vertical_button = widgets.get_widget('Editor:FlipVerticalButton')
        flip_horizontal_button = widgets.get_widget('Editor:FlipHorizontalButton')
        scale_button = widgets.get_widget('Editor:ScaleButton')
        cancel_button = widgets.get_widget('Editor:TransformCancelButton')
        
        # Connect signals and slots.
        rotate_left_button.connect('clicked', self.rotate_left_button_clicked)
        rotate_right_button.connect('clicked', self.rotate_right_button_clicked)
        flip_vertical_button.connect('clicked', self.flip_vertical_button_clicked)
        flip_horizontal_button.connect('clicked', self.flip_horizontal_button_clicked)
        scale_button.connect('clicked', self.scale_button_clicked)
        cancel_button.connect('clicked', self.cancel_button_clicked)
        
    def rotate_left_button_clicked(self, widget):
        self.editor.set_view_image(self.editor.view_image.rotate(90))
        self.editor.add_operation(('rotate', 90))
        self.editor.transform_vbox.hide()
        self.editor.main_vbox.show()
    
    def rotate_right_button_clicked(self, widget):
        self.editor.set_view_image(self.editor.view_image.rotate(270))
        self.editor.add_operation(('rotate', 270))
        self.editor.transform_vbox.hide()
        self.editor.main_vbox.show()
    
    def flip_vertical_button_clicked(self, widget):
        self.editor.set_view_image(self.editor.view_image.transpose(Image.FLIP_LEFT_RIGHT))
        self.editor.add_operation(('flip_vertical', None))
        self.editor.transform_vbox.hide()
        self.editor.main_vbox.show()
    
    def flip_horizontal_button_clicked(self, widget):
        self.editor.set_view_image(self.editor.view_image.transpose(Image.FLIP_TOP_BOTTOM))
        self.editor.add_operation(('flip_horizontal', None))
        self.editor.transform_vbox.hide()
        self.editor.main_vbox.show()
    
    def scale_button_clicked(self, widget):
        # Pop up the resize dialog.
        sd = ResizeDialog()
        (size, maintain_aspect) = sd.do_it()
        
        # Add scale operation to operations stack.
        if size:
            # If maintain aspect is set we find the correct size of the image now.
            if maintain_aspect:
                size = ImageHandling.adjust_size_to_maintain_aspect(self.editor.original_image.size, size)
            self.editor.add_operation(('scale', size))
        self.editor.transform_vbox.hide()
        self.editor.main_vbox.show()
    
    def cancel_button_clicked(self, widget):
        self.editor.transform_vbox.hide()
        self.editor.main_vbox.show()
            